package com.k.demo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.k.demo.entity.Food;
import com.k.demo.mapper.FoodMapper;
import com.k.demo.service.FoodService;

@Service
public class FoodServiceImpl implements FoodService{

	@Autowired
	private FoodMapper foodMapper;
	
	@Override
	public List<Food> getAll() {
		// TODO Auto-generated method stub
		return foodMapper.selectAll();
	}
	
	public List<Food> searchFoodByKind(int food_kind_id){
		return foodMapper.selectByKind(food_kind_id);
	}
	
	public List<Food> searchFoodByNameAndKind(Food food){
		return foodMapper.selectByNameAndKind(food);
	}
	
	public Food detail(int food_id) {
		return foodMapper.selectById(food_id);
	}
	
	public PageInfo<Food> getpage(int pageNum,int pageSize){
		PageHelper.startPage(pageNum,pageSize);
		return new PageInfo<Food>(foodMapper.selectAll());
	}
	public PageInfo<Food> getpagebykind(int pageNum,int pageSize,int food_kind_id){
		PageHelper.startPage(pageNum,pageSize);
		return new PageInfo<Food>(foodMapper.selectByKind(food_kind_id));
	}
	public PageInfo<Food> getpagebyname(int pageNum,int pageSize,String food_name){
		PageHelper.startPage(pageNum,pageSize);
		return new PageInfo<Food>(foodMapper.selectByName(food_name));
	}

	@Override
	public int addFood(Food food) {
		// TODO Auto-generated method stub
		return foodMapper.insert(food);
	}

	@Override
	public int updateFood(Food temp) {
		// TODO Auto-generated method stub
		return foodMapper.update(temp);
	}

	@Override
	public int deleteForEach(List<Integer> deletelist) {
		// TODO Auto-generated method stub
		return foodMapper.deleteForEach(deletelist);
	}

	@Override
	public PageInfo<Food> getpagebynameandkind(int pageNum, int pageSize, Food food) {
		PageHelper.startPage(pageNum,pageSize);
		return new PageInfo<Food>(foodMapper.selectByNameAndKind(food));
	}

	@Override
	public int delete(int food_id) {
		// TODO Auto-generated method stub
		return foodMapper.delete(food_id);
	}

	//分页返回排序结果
	public PageInfo<Food> getAllByOrderWithPage(int pageNum, int pageSize, String orderBy) {
		
		PageHelper.startPage(pageNum,pageSize);
		
		return new PageInfo<Food>(foodMapper.getFoodByOrder(orderBy));
	}

	//返回食物的模糊查询
	public PageInfo<Food> getFoodByFoodWithPage(int pageNum, int pageSize, Food food,Double lowestPrice,Double highestPrice,String orderBy) {
		
		PageHelper.startPage(pageNum,pageSize);
		
		return new PageInfo<Food>(foodMapper.getFoodByFood(food,lowestPrice,highestPrice,orderBy));
	}
}
